#pragma once
#ifndef __PROJECT__FILE__HH__
#define __PROJECT__FILE__HH__

#include "MaterialPrerequisites.h"
#include "MaterialWindow.h"
#include "ui_createitem.h"

#define MATERIAL_RECENTS_FILE "material_editor.rct"
#define MATERIAL_RECENTS_MAX 5

namespace Lit
{
	namespace Editor
	{
		namespace Material
		{
			class Project;
			class ProjectXmlSerialiser;
			class RecentProjectList
			{
				std::vector<FilePath> d;
			public:
				RecentProjectList();
				virtual ~RecentProjectList();

				bool load( );
				bool save( );
				
				bool has( const FilePath &path ) const;

				std::vector<FilePath>::iterator begin();
				std::vector<FilePath>::iterator end();
				
				std::vector<FilePath>::const_iterator begin()const;
				std::vector<FilePath>::const_iterator end() const;

				std::vector<FilePath>::iterator find( const FilePath &path );
				std::vector<FilePath>::const_iterator find( const FilePath &path ) const;

				void add( const FilePath &path );
				bool empty() const;
				void clear();
				unsigned short size() const;

				FilePath lastProject( ) const;
				std::vector<FilePath> lastProjects( const unsigned short upper_lim = MATERIAL_RECENTS_MAX ) const;
			};
			struct ProjectFileDefinition
			{
				ProjectFileDefinition() : accepted(false){}
				ProjectFileDefinition(const std::string &name_, const std::string &extension_, bool accepted_=false, const std::string &directory_="./", const std::string &category_="", const std::string &content_="" )
					: name( name_ )
					, extension( extension_ )
					, directory( directory_ )
					, category( category_ )
					, content( content_ )
					, accepted(accepted_){}
				~ProjectFileDefinition(){}

				std::string fullName() const{ return name + "." + extension; }
				std::string fullPath() const{ return directory + "/" + fullName(); }

				std::string name;
				std::string extension;
				std::string directory;
				std::string category;
				std::string content;
				bool accepted;
			};
			class ProjectItemCreator : public QDialog
			{
				Q_OBJECT
			protected:
				Ui::CreateItemClass ui;
				Project* mProject;
				QTreeWidgetItem * mAllNode,
								* mOgreNode,
								* mResNode,
								* mOtherNode;
			protected slots:
				void browseDirectory();
				void updateItemType(QTreeWidgetItem* itm, int c);
			public:
				ProjectItemCreator ( QWidget * parent );
				~ProjectItemCreator();
				
				ProjectFileDefinition currentDefinition(bool accepted = true) const;

				ProjectFileDefinition define( 
					const std::string &name, 
					const std::string &extension, 
					bool accepted, 
					const std::string &directory, 
					const std::string &category, 
					const std::string &content ) const;

				void setupUi();
				std::vector<ProjectFileDefinition> exec( Project * proj );
			};
			class ProjectFile
			{
				friend class ProjectXmlSerialiser;

			public:
				ProjectFile( const std::string &nam, Project * proj ) ;
				~ProjectFile();
			
				std::string getName() const;
			
				void setText( const std::string &text );
				std::string getText() const;
			
				bool isModified() const;
				void setModified( bool state );
			
				void save( const Lit::FilePath& base_dir = Material::UserPath() );
				void load( const Lit::FilePath& base_dir = Material::UserPath() );

				TextEdit * getCodeEditor();
				const TextEdit * getCodeEditor() const;

				EditorWidget * getEditor();
				const EditorWidget * getEditor() const;

				const Window* getWindow() const;
				Window* getWindow();

				const QTreeWidgetItem* getItem() const;
				QTreeWidgetItem* getItem() ;

				const Project* getProject() const;
				Project* getProject();

			protected:
				Project * project;
				Window * window;
				QTreeWidgetItem * item;
				std::string name;
			};
			typedef RefPtr<ProjectFile> ProjectFilePtr;
		}
	};
};

#endif